package com.bianguo.android.bprinter.entity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.view.View;

import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.bprinter.R;
import com.bianguo.android.bprinter.utils.RectUtil;

import java.util.ArrayList;

public class ZoomBean2 {

    private RectF mRect;
    private Bitmap deleteBitmap, mirrorBitmap, cutBitmap, scaleBitmap;
    private Matrix matrix;
    private PointF deletePoint, mirrorPoint, scalePoint, cutPoint;
    private Bitmap srcBitmap;
    private PointF centerPoint;

    public ZoomBean2(Context mContext, Matrix matrix) {
        deleteBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_delete_icon);
        mirrorBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_mirror_icon);
        cutBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_cut_icon);
        scaleBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_scale_icon);
        this.matrix = matrix;

        deletePoint = new PointF();
        mirrorPoint = new PointF();
        scalePoint = new PointF();
        cutPoint = new PointF();
    }

    public void setSrcBitmap(Bitmap bitmap) {
        this.srcBitmap = bitmap;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        deletePoint.x = -deleteBitmap.getWidth() / 2;
        deletePoint.y = -deleteBitmap.getHeight() / 2;

        mirrorPoint.x = width - mirrorBitmap.getWidth() / 2;
        mirrorPoint.y = -mirrorBitmap.getHeight() / 2;

        scalePoint.x = width- scaleBitmap.getWidth() / 2;
        scalePoint.y = height - scaleBitmap.getHeight() / 2;

        cutPoint.x = -cutBitmap.getWidth() / 2;
        cutPoint.y = height - cutBitmap.getHeight() / 2;

        mRect.left = 0;
        mRect.top = 0;
        mRect.right = width;
        mRect.bottom = height;

        centerPoint = new PointF();
        centerPoint.x = mRect.centerX();
        centerPoint.y = mRect.centerY();
    }

    public void updateBitmap(Bitmap bitmap) {
        this.srcBitmap = bitmap;
    }

    /**
     * 判断手指是否在图片区域内
     * @param x
     * @param y
     * @return
     */
    public boolean contains(float x, float y) {
        float minX = 10000;
        float minY = 10000;
        float maxX = 0;
        float maxY = 0;
        float[] arrayX = {deletePoint.x, mirrorPoint.x, scalePoint.x, cutPoint.x};
        float[] arrayY = {deletePoint.y, mirrorPoint.y, scalePoint.y, cutPoint.y};
        for (int i = 0; i < arrayX.length; i++) {
            if (arrayX[i] < minX) {
                minX = arrayX[i];
            }
            if (arrayX[i] > maxX) {
                maxX = arrayX[i];
            }
        }

        for (int i = 0; i < arrayY.length; i++) {
            if (arrayY[i] < minY) {
                minY = arrayY[i];
            }
            if (arrayY[i] > maxY) {
                maxY = arrayY[i];
            }
        }
        return x >= minX && x <= maxX && y >= minY && y <= maxY;
    }

    /**
     * 判断是否是否在单指缩放的图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsScale(float x, float y) {
        return x >= scalePoint.x && x <= scalePoint.x + scaleBitmap.getWidth() &&
                y >= scalePoint.y && y <= scalePoint.y + scaleBitmap.getHeight();
    }

    /**
     * 判断是否在镜像图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsMirror(float x, float y) {
        return x >= mirrorPoint.x && x <= mirrorPoint.x + mirrorBitmap.getWidth() &&
                y >= mirrorPoint.y && y <= mirrorPoint.y + mirrorBitmap.getHeight();
    }

    /**
     * 判断是否在删除图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsDelete(float x, float y) {
        return x >= deletePoint.x && x <= deletePoint.x + deleteBitmap.getWidth() &&
                y >= deletePoint.y && y <= deletePoint.y + deleteBitmap.getHeight();

    }

    /**
     * 判断是否在剪切图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsCut(float x, float y) {
        return x >= cutPoint.x && x <= cutPoint.x + cutBitmap.getWidth() &&
                y >= cutPoint.y && y <= cutPoint.y + cutBitmap.getHeight();
    }

    /**
     * 获取View的中心坐标
     *
     * @return
     */
    public PointF getCenterPoint() {
        if (true) {
            return centerPoint;
        }
        PointF pointF = new PointF();
//        pointF.x = (scalePoint.x + scaleBitmap.getWidth() / 2 + deletePoint.x + deleteBitmap.getWidth() / 2) / 2;
//        pointF.y = (scalePoint.y + scaleBitmap.getHeight() / 2 + deletePoint.y + deleteBitmap.getHeight() / 2) / 2;
        pointF.x = mRect.centerX();
        pointF.y = mRect.centerY();
        return pointF;
    }

    public PointF getBottomPoint() {
        PointF pointF = deletePoint;
        ArrayList<PointF> points = new ArrayList<>();
        points.add(deletePoint);
        points.add(mirrorPoint);
        points.add(scalePoint);
        points.add(cutPoint);
        for (PointF point : points) {
            if (pointF.y < point.y) {
                pointF = point;
            }
        }
        return pointF;
    }

    public void drawRect(Canvas canvas, Paint mPaint) {
//        canvas.drawRect(mRect, mPaint);
        canvas.drawLine(deletePoint.x + deleteBitmap.getWidth() / 2, deletePoint.y + deleteBitmap.getHeight() / 2, mirrorPoint.x + mirrorBitmap.getWidth() / 2, mirrorPoint.y + mirrorBitmap.getHeight() / 2, mPaint);
        canvas.drawLine(deletePoint.x + deleteBitmap.getWidth() / 2, deletePoint.y + deleteBitmap.getHeight() / 2, cutPoint.x + cutBitmap.getWidth() / 2, cutPoint.y + cutBitmap.getHeight() / 2, mPaint);
        canvas.drawLine(mirrorPoint.x + mirrorBitmap.getWidth() / 2, mirrorPoint.y + mirrorBitmap.getHeight() / 2, scalePoint.x + scaleBitmap.getWidth() / 2, scalePoint.y + scaleBitmap.getHeight() / 2, mPaint);
        canvas.drawLine(scalePoint.x + scaleBitmap.getWidth() / 2, scalePoint.y + scaleBitmap.getHeight() / 2, cutPoint.x + cutBitmap.getWidth() / 2, cutPoint.y + cutBitmap.getHeight() / 2, mPaint);
    }

    public void drawRect(Canvas canvas, Paint mPaint, float rotation) {
        canvas.save();
        PointF centerPoint = getCenterPoint();
        canvas.rotate(rotation, centerPoint.x, centerPoint.y);
        canvas.drawRect(mRect, mPaint);
        canvas.restore();
    }

    public void drawBitmap(Canvas canvas, Paint mPaint) {
        canvas.drawBitmap(srcBitmap, matrix, mPaint);
    }

    public void drawPoint(Canvas canvas, Paint mPaint) {
        //左上角
//        canvas.drawBitmap(deleteBitmap, mRect.left - deleteBitmap.getWidth() / 2, mRect.top - deleteBitmap.getHeight() / 2, null);
        canvas.drawBitmap(deleteBitmap, deletePoint.x, deletePoint.y, mPaint);
//        canvas.drawBitmap(deleteBitmap, deleteMatrix, mPaint);
        //右上角
//        canvas.drawBitmap(mirrorBitmap, mRect.right - mirrorBitmap.getWidth() / 2, mRect.top - mirrorBitmap.getHeight() / 2, null);
        canvas.drawBitmap(mirrorBitmap, mirrorPoint.x, mirrorPoint.y, mPaint);
//        canvas.drawBitmap(mirrorBitmap, mirrorMatrix, mPaint);
        //右下角
//        canvas.drawBitmap(scaleBitmap, mRect.right - scaleBitmap.getWidth() / 2, mRect.bottom - scaleBitmap.getHeight() / 2, null);
        canvas.drawBitmap(scaleBitmap, scalePoint.x, scalePoint.y, mPaint);
//        canvas.drawBitmap(scaleBitmap, scaleMatrix, mPaint);
        //左下角
//        canvas.drawBitmap(cutBitmap, mRect.left - cutBitmap.getWidth() / 2, mRect.bottom - cutBitmap.getHeight() / 2, null);
        canvas.drawBitmap(cutBitmap, cutPoint.x, cutPoint.y, mPaint);
//        canvas.drawBitmap(cutBitmap, cutMatrix, mPaint);
    }

    public RectF getRect() {
        return mRect;
    }

    public void setRect(RectF mRect) {
        this.mRect = mRect;
    }

    /**
     * 根据移动坐标更新View
     *
     * @param translateX
     * @param translateY
     */
    public void updatePoint(float translateX, float translateY) {
        deletePoint.x =  deletePoint.x + translateX;
        deletePoint.y =  deletePoint.y + translateY;

        mirrorPoint.x =  mirrorPoint.x + translateX;
        mirrorPoint.y =  mirrorPoint.y + translateY;

        scalePoint.x =  scalePoint.x + translateX;
        scalePoint.y =  scalePoint.y + translateY;

        cutPoint.x =  cutPoint.x + translateX;
        cutPoint.y =  cutPoint.y + translateY;

//        updateRect();
        centerPoint.x = centerPoint.x + translateX;
        centerPoint.y = centerPoint.y + translateY;
    } 

    /**
     * 移动到左上方，在第一次创建的时候调用
     * @param matrix
     */
    public void translate2Dot(Matrix matrix) {
        matrix.postTranslate(-deletePoint.x, -deletePoint.y);
        updatePoint(-deletePoint.x, -deletePoint.y);
    }

    /**
     * 根据缩放系数更新View
     *
     * @param scale
     */
    public void updatePoint(float scale) {

        float w = mRect.width();
        float h = mRect.height();

        float newW = scale * w;
        float newH = scale * h;

        float dx = (newW - w) / 2;
        float dy = (newH - h) / 2;

        mRect.left -= dx;
        mRect.top -= dy;
        mRect.right += dx;
        mRect.bottom += dy;

//        updatePoint();
    }

    public void adjustLocation(Matrix matrix) {
        //将有缩放平移和旋转相关值的矩阵赋值到f中
        float[] f = new float[9];
        matrix.getValues(f);
        int bWidth = 0;
        int bHeight = 0;

        //取到view的宽高
        bWidth = (int) mRect.width();
        bHeight = (int) mRect.height();
        //如果想知道这里这样设置值的具体算法那必须要了解9*9的矩阵每个坐标的含义了 有兴趣的可以查阅一下 资料很多
        // 原图左上角
        float x1 = f[2];
        float y1 = f[5];
        // 原图右上角
        float x2 = f[0] * bWidth + f[2];
        float y2 = f[3] * bWidth + f[5];
        // 原图左下角
        float x3 = f[1] * bHeight + f[2];
        float y3 = f[4] * bHeight + f[5];
        // 原图右下角
        float x4 = f[0] * bWidth + f[1] * bHeight + f[2];
        float y4 = f[3] * bWidth + f[4] * bHeight + f[5];
        deletePoint.set(x1 - deleteBitmap.getWidth() / 2, y1 - deleteBitmap.getHeight() / 2);
        mirrorPoint.set(x2 - mirrorBitmap.getWidth() / 2, y2 - mirrorBitmap.getHeight() / 2);
        cutPoint.set(x3 - cutBitmap.getWidth() / 2, y3 - cutBitmap.getHeight() / 2);
        scalePoint.set(x4 - scaleBitmap.getWidth() / 2, y4 - scaleBitmap.getHeight() / 2);
    }

    public void updatePoint(PointF centerPoint, float rotation) {
        RectUtil.rotatePoint(deletePoint, centerPoint.x, centerPoint.y, rotation);
        RectUtil.rotatePoint(mirrorPoint, centerPoint.x, centerPoint.y, rotation);
        RectUtil.rotatePoint(scalePoint, centerPoint.x, centerPoint.y, rotation);
        RectUtil.rotatePoint(cutPoint, centerPoint.x, centerPoint.y, rotation);
        RectUtil.rotateRect(mRect, centerPoint.x, centerPoint.y, rotation);
    }

    /**
     * 根据Point更新Rect
     */
    private void updateRect() {
        mRect.left = (int) (deletePoint.x + deleteBitmap.getWidth() / 2);
        mRect.right = (int) (scalePoint.x + scaleBitmap.getWidth() / 2);
        mRect.top = (int) (deletePoint.y + deleteBitmap.getHeight() / 2);
        mRect.bottom = (int) (scalePoint.y + scaleBitmap.getHeight() / 2);
    }

    /**
     * 根据Rect更新Point
     */
    private void updatePoint() {
        deletePoint.x = mRect.left - deleteBitmap.getWidth() / 2;
        deletePoint.y = mRect.top - deleteBitmap.getHeight() / 2;

        mirrorPoint.x = mRect.right - mirrorBitmap.getWidth() / 2;
        mirrorPoint.y = mRect.top - mirrorBitmap.getHeight() / 2;

        scalePoint.x = mRect.right - scaleBitmap.getWidth() / 2;
        scalePoint.y = mRect.bottom - scaleBitmap.getHeight() / 2;

        cutPoint.x = mRect.left - cutBitmap.getWidth() / 2;
        cutPoint.y = mRect.bottom - cutBitmap.getHeight() / 2;
    }

    public int getWidth() {
        return (int) mRect.width();
    }

    public int getHeight() {
        return (int) mRect.height();
    }
}
